package com.zzld.modules.autoinn.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.test.annotation.Repeat;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zzld.modules.autoinn.entity.Facilitator;
import com.zzld.modules.autoinn.service.IStoreService;
import com.zzld.modules.common.SessionUser;
import com.zzld.modules.common.dto.EasyuiTree;
import com.zzld.modules.sys.entity.Res;

@Controller
@RequestMapping("/autoinn/wxfacilitator/res")
public class WXFacilitatorresController {
	
	@Autowired
	private IStoreService storeService;
	
	private String adminlist="/modules/autoinn/facilitator/adminList";
	private String dinglist="/modules/autoinn/facilitator/dinglist";
	private String xiaolist="/modules/autoinn/facilitator/xiaolist";
	private String showpass="/modules/autoinn/facilitator/showmypass";
	private String baolist ="/modules/autoinn/facilitator/matnList";
	
	
	@RequestMapping("pass")
	 public String showpass(HttpServletRequest request){
		if(getli(request)==null){
			return "/modules/autoinn/facilitator/login";
		}
		return showpass;
	}
	
	@RequestMapping("adminlist")
    public String adminlist(HttpServletRequest request,Model model){
		if(getli(request)==null){
			return "/modules/autoinn/facilitator/login";
		}
	    Facilitator fa = (Facilitator)request.getSession().getAttribute("fa");
	    model.addAttribute("storid", fa.getStoreid());
		return adminlist;
	}
	
	
	@RequestMapping("baolist")
    public String baolist(HttpServletRequest request,Model model){
		if(getli(request)==null){
			return "/modules/autoinn/facilitator/login";
		}
	    Facilitator fa = (Facilitator)request.getSession().getAttribute("fa");
	    model.addAttribute("storid", fa.getStoreid());
		return baolist;
	}
	
	@RequestMapping("dinglist")
    public String dinglist(HttpServletRequest request){
		if(getli(request)==null){
			return "/modules/autoinn/facilitator/login";
		}
		return dinglist;
	}
	
	@RequestMapping("xiaolist")
    public String xiaolist(HttpServletRequest request){
		if(getli(request)==null){
			return "/modules/autoinn/facilitator/login";
		}
		return xiaolist;
	}
	
	
	@RequestMapping(value="/getres",method=RequestMethod.POST)
	@ResponseBody
	private List<EasyuiTree> getUserResTreeById(
			@RequestParam(value = "pid") String pid,HttpServletRequest request) {
		List<EasyuiTree> nodes = new ArrayList<EasyuiTree>();
		try {
			List<Res> resList = getlist(pid,request);
			if (resList == null) {
				return null;
			}
			for (Res res : resList) {
				EasyuiTree node = new EasyuiTree();
				node.setId(res.getId());
				node.setText(res.getName());
				if (!StringUtils.isBlank(res.getPid())) {
					node.setPid(res.getPid());
				}
				Map<String, String> url = new HashMap<String, String>();
				url.put("url", res.getResurl());
				node.setAttributes(url);
				nodes.add(node);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return nodes;
	}
	
	
	public List<Res> getlist(String pid,HttpServletRequest request){
		List<Res> list = new ArrayList<Res>();
		
		Facilitator facilitator =(Facilitator)request.getSession().getAttribute("fa");
		if(facilitator.getType()==0){
		
		Res  r = new Res();
		r.setId("1");
		r.setName("服务商管理");
		r.setPid("null");
		r.setResurl("#");
		list.add(r);
		
		Res  r1 = new Res();
		r1.setId("2");
		r1.setName("管理员管理");
		r1.setPid("1");
		r1.setResurl("/autoinn/wxfacilitator/res/adminlist");
		list.add(r1);
		}
		Res r2 = new Res();
		r2.setId("3");
		r2.setName("洗车订单管理");
		r2.setPid("null");
		r2.setResurl("#");
	    list.add(r2);
	    
		Res r3 = new Res();
		r3.setId("4");
		r3.setName("订单列表");
		r3.setPid("3");
		r3.setResurl("/autoinn/wxfacilitator/res/dinglist");
	    list.add(r3);
	    
		Res r4 = new Res();
		r4.setId("5");
		r4.setName("销量情况");
		r4.setPid("3");
		r4.setResurl("/autoinn/wxfacilitator/res/xiaolist");
	    list.add(r4);
	  
	    String isma = storeService.selecthoukey(facilitator.getStoreid()).get("ismaintenance").toString();
	    
	    if(StringUtils.isNotBlank(isma)&&!isma.equals("0")){
	    	Res r5 = new Res();
			r5.setId("6");
			r5.setName("保养订单管理");
			r5.setPid("null");
			r5.setResurl("#");
		    list.add(r5);
		    
		    Res r6 = new Res();
			r6.setId("7");
			r6.setName("保养订单列表");
			r6.setPid("6");
			r6.setResurl("/autoinn/wxfacilitator/res/baolist");
		    list.add(r6);
	    }
	    
		List<Res> li = new ArrayList<Res>();
			for(int i=0;i<list.size();i++){
				if(list.get(i).getPid().equals(pid)){
					li.add(list.get(i));
				}
			}
		
		
		return li;
		
	}
	
	private Facilitator getli(HttpServletRequest request){
		Facilitator fa =null;
		try{
			fa = (Facilitator)request.getSession().getAttribute("fa");
		}catch(Exception e){
			e.printStackTrace();
		}
		return fa;
	}
	
}
